import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path:'/', redirect:'/login'},
  { path:'/login', component:() => import('@/views/Login') },
  { path:'/Main', component:() => import('@/views/Main'),
    children:[
      { path:'/users', component:() => import('@/views/Main/Users') },
      { path:'/roles', component:() => import('@/views/Main/Roles') },
      { path:'/rights', component:() => import('@/views/Main/Rights') },
      { path:'/goods', component:() => import('@/views/Main/Goods') },
      { path:'/goods', component:() => import('@/views/Main/Goods') },
      { path:'/goods/add', component:() => import('@/views/Main/Goods/addGood') },
      { path:'/params', component:() => import('@/views/Main/Params') },
      { path:'/categories', component:() => import('@/views/Main/Categories') },
      { path:'/orders', component:() => import('@/views/Main/Orders') },
      { path:'/reports', component:() => import('@/views/Main/Reports') },
    ]
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach( ( to, from, next )=> {
  if( to.path === '/login' ) return next()
  let token = window.sessionStorage.getItem('token')
  if( !token ) return next('/login')
  next()
})

export default router
